package com.example.teacherservice.controller;

import com.example.teacherservice.dto.*;
import com.example.teacherservice.service.LearningProgressService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/learning-progress")
@RequiredArgsConstructor
public class LearningProgressController {

    private final LearningProgressService progressService;

    @PostMapping("/update")
    public ResponseEntity<Map<String, Object>> updateProgress(@RequestBody ProgressUpdateDTO dto) {
        progressService.updateLearningProgress(dto);

        // 明确返回JSON响应体
        return ResponseEntity.ok()
                .contentType(MediaType.APPLICATION_JSON)
                .body(Map.of(
                        "code", 200,
                        "success", true,
                        "data", dto
                ));
    }

    @GetMapping("/chapter")
    public ResponseEntity<ProgressResponseDTO> getChapterProgress(
            @RequestParam Long studentId,
            @RequestParam Long courseId,
            @RequestParam Long chapterId) {
        ProgressResponseDTO response = progressService.getChapterProgress(studentId, courseId, chapterId);
        return ResponseEntity.ok(response);
    }

    @GetMapping("/course")
    public ResponseEntity<CourseProgressDTO> getCourseProgress(
            @RequestParam Long studentId,
            @RequestParam Long courseId) {
        CourseProgressDTO response = progressService.getCourseProgress(studentId, courseId);
        return ResponseEntity.ok(response);
    }

    @PostMapping("/complete")
    public ResponseEntity<Void> markChapterAsCompleted(
            @RequestParam Long studentId,
            @RequestParam Long courseId,
            @RequestParam Long chapterId) {
        progressService.markChapterAsCompleted(studentId, courseId, chapterId);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/batch-chapters")
    public ResponseEntity<List<ProgressResponseDTO>> getBatchChapterProgress(
            @RequestParam Long studentId,
            @RequestParam Long courseId,
            @RequestParam List<Long> chapterIds) {
        List<ProgressResponseDTO> responses = progressService.getBatchChapterProgress(studentId, courseId, chapterIds);
        return ResponseEntity.ok(responses);
    }

    @GetMapping("/all-courses")
    public ResponseEntity<StudentAllCoursesProgressDTO> getAllCoursesProgress(
            @RequestParam Long studentId) {
        StudentAllCoursesProgressDTO response = progressService.getAllCoursesProgress(studentId);
        return ResponseEntity.ok(response);
    }
}